package com.moliku.insurance.process.strategy;

import com.moliku.insurance.common.constant.CompanyEnum;
import com.moliku.insurance.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 策略工厂
 *
 * @author Mr.Liu
 * @since 2021/2/5
 */
@Slf4j
@Service
public class StrategyFactory {

    @Resource
    private final Map<String, PolicyStrategy> strategyMap = new ConcurrentHashMap<>(15);

    /**
     * 使用策略工厂获取具体策略实现
     *
     * @param company 公司编码
     *
     *
     * @return 策略实现
     */
    public PolicyStrategy getInstance(String company) {
        String component = CompanyEnum.getStrategy(company);
        PolicyStrategy strategy = strategyMap.get(component);
        if (strategy == null) {
            throw new ServiceException("保险公司编码不正确,【" + company + "】");
        }
        return strategy;
    }
}
